<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class account_token_model extends MY_Model {
	protected $table = 'account_token';

	public function get_one($where, $fields = "*", $table = FALSE) {
		if (!$table) {
			$table = $this->table;
		}
		if (!$where) {
			return FALSE;
		}

		$this->db->select($fields)->from($table);
		if (!is_numeric($where)) {
			$this->db->where($where);
		} else {
			$this->db->where('token', $where);
		}

		$query = $this->db->get();
		$row = $query->row_array();
		return $row;
	}
	public function delete($key,$where=false) {
		if (is_numeric($key)) {
			$this->db->where(array('accountId'=>$key));
		}

		if (is_array($key)) {
			$this->db->where_in('accountId',$key);
		}

		if ($where) {
			$this->db->where($where);
		}

		$this->db->delete($this->table);
		return $this->db->affected_rows();
	}
	public function gettoken_admin($accountId, $fresh = 'nofresh', $terminalNo = 0) {
		$token = genToken();
		$terminalNo = 3;
		$token_now = $this->get_one(array('accountId'=>$accountId), '*', 'account_token');
		if ($token_now) {
			if ($fresh === 'nofresh') {
				$token = $token_now['token'];
			} else {
				$this->update(array('token' => $token, 'expiretime'=>time()+(TOKEN_TIME_EXPIRE*24*3600)), array('accountId' => $accountId, 'terminalNo' => $terminalNo), false, 'account_token');
			}
		} else {
			$this->create(array('token' => $token, 'expiretime'=>time()+(TOKEN_TIME_EXPIRE*24*3600), 'accountId' => $accountId, 'terminalNo' => $terminalNo), false, 'account_token');
		}

		return $token;
	}
}
